let app = getApp();
let url = app.globalData.url;
let storeId;
let merchantId;
let userId;
Page({
    data: {
        items: "",
        storeId: "",
        merchantId: "",
        none: "none",
        count: '',
        color_left: "#ff7950",
        color_right: "#333",
        left_dis: "",
        rit_dis: "none",
        quan: [],
        background:"",
        dishPic:"",
        couponName: "",
        dishName: "",
        none1:"none"
    },
    onLoad(ele) {
        wx.hideShareMenu();
        this.agree();
        wx.showNavigationBarLoading()
    },
    agree() {
        var that = this
        // 获取merchantId
        wx.getStorage({
            key: 'merchantId',
            success: function (res) {
                merchantId = res.data.merchantId;
                wx.getStorage({
                    key: 'storeId',
                    success: function (res) {
                        storeId = res.data.storeId;

                        wx.getStorage({
                            key: 'userId',
                            success: function (res) {
                                userId = res.data.userId;
                                that.cark();
                            },
                            fail: function (res) {
                                // my.alert({content: "请求失败"});
                            }
                        });
                    },
                    fail: function (res) {
                        // my.alert({content: "请求失败"});
                    }
                });
            },
            fail: function (res) {
                // my.alert({content: "请求失败"});
            }
        });
    },
    cark() {
        var that = this;
        wx.request({
            url: url + '/couponTemplate/getList.do',
            method: 'get',
            data: {
                merchantId: merchantId,
                storeId: storeId,
                userId: userId,
            },
            headers: {
                'Content-Type': 'application/json;charset=UTF-8',
                'Accept': 'application/json',
            },
            dataType: 'json',
            success: function (res) {
                console.log(res);
                if (res.data.body == "" || res.data.body == []) {
                    that.setData({
                        none: "block"
                    });
                    return false;
                } else {

                    var arr = [];
                    var data = res.data.body;
                    for (var i in data) {
                        data[i].name = data[i].name;
                        data[i].effectiveness = data[i].couponValidDateEnd;
                        data[i].img = data[i].merchantLogo;
                        data[i].merchantName = data[i].merchantName;
                        data[i].count = data[i].count
                        // data[i].amount=data[i].amount;
                        // data[i].benefit=data[i].benefit;
                        // if(data[i].status==1){
                        //   data[i].receive="可使用";
                        // }else if(data[i].status==2){
                        //   data[i].receive="已使用";
                        // }else if(data[i].status==3){
                        //   data[i].receive="已过期";
                        // }
                        if (data[i].type == 1) {
                            if (data[i].amount > 0) {
                                data[i].you = "满" + data[i].amount + "元减" + data[i].benefit + "元";
                                data[i].zeng_you = "赠" + data[i].benefit + "元"
                            } else if (data[i].amount == 0) {
                                data[i].you = data[i].benefit + "元代金券";
                                data[i].zeng_you = "赠" + data[i].benefit + "元"
                            }
                        } else if (data[i].type == 2) {
                            data[i].you = "折扣券"
                            data[i].zeng_you = "打" + data[i].benefit * 0.1 + "折"
                        }
                        console.log(data[i].effectiveness);
                        arr.push(data[i]);
                    }
                    that.setData({
                        items: arr,
                        none: "none"
                    });


                }
                wx.hideNavigationBarLoading()
            },
            fail: function (res) {
                wx.hideNavigationBarLoading()
            },
            complete: function (res) {
                wx.hideLoading();
                wx.hideNavigationBarLoading()
            }

        })
    },
    //   点击商品券
    shop() {
        var that = this;
        wx.getStorage({
            key: 'merchantId',
            success: function (res) {
                var merchantId = res.data.merchantId;
        wx.getStorage({
            key: 'userId',
            success: function (res) {
                var userId = res.data.userId;
                wx.request({
                    url: url + '/foreignDishCoupon/getDishCouponList.do',
                    method: 'get',
                    data: {
                        userId: userId,
                        merchantId: merchantId
                    },
                    headers: {
                        'Content-Type': 'application/json;charset=UTF-8',
                        'Accept': 'application/json',
                    },
                    dataType: 'json',
                    success: function (res) {
                        if (res.data.body == "" || res.data.body == []) {
                            that.setData({
                                none1: "block"
                            });
                            return false;
                        } else {
                        var arr=[];
                        var body = res.data.body;
                        for (var i = 0; i < body.length;i++){
                            body[i].couponTimeEnd = body[i].couponTimeEnd
                            body[i].couponName = body[i].couponName
                            body[i].dishName = body[i].dishName
                            if (body[i].status==1){
                                body[i].use="未使用",
                                    body[i].background="#ff7950"
                            } else if (body[i].status == 2){
                                body[i].use = "已使用",
                                    body[i].background = "#aeaeae"
                            }
                            arr.push(body[i])
                        }
                          
                        that.setData({
                            quan: arr
                        })
                        }
                    },
                    fail: function (res) {

                    },
                    complete: function (res) {

                    }
                });
            }
        })
            }
        })
        this.setData({
            color_left: "#333",
            color_right: "#ff7950",
            left_dis: "none",
            rit_dis: "block",
        })
    },
    discount() {
        this.setData({
            color_left: "#ff7950",
            color_right: "#333",
            left_dis: "block",
            rit_dis: "none",
        })
    },
    coupon_details(eve) {
        wx.navigateTo({
            url: '../coupon_details/coupon_details?shop_id=' + eve.currentTarget.dataset.id,
        })
    },
    onPullDownRefresh() {
        var that = this;
        that.cark();
        wx.stopPullDownRefresh();
    },
});
